package user_views

import (
	"github.com/labstack/echo/v4"
	"github.com/skip2/go-qrcode"
	"net/http"
	"rabbit_love/src/service/client/daos/user_daos"
	"rabbit_love/src/service/client/forms/request/user_forms"
	"rabbit_love/src/service/client/utils"
	"rabbit_love/src/utils/response"
)

func LoginApi(ctx echo.Context) error {
	var (
		form user_forms.LoginRequest
		err  error
	)
	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var token string
	token, err = user_daos.LoginDao(&form)
	if err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "登陆成功", token)
}

func ValCodeApi(ctx echo.Context) error {
	var (
		form user_forms.RegisterValCodeRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	if err = user_daos.ValCodeDao(&form); err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "操作成功", "")
}

func RegisterApi(ctx echo.Context) error {
	var (
		form user_forms.RegisterRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var token string
	token, err = user_daos.RegisterDao(&form)
	if err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "注册成功", token)
}

func UserInfoApi(ctx echo.Context) error {
	var (
		userInfos map[string]interface{}
		err       error
	)

	/*
		TODO；
			基础信息(用户名、头像)
			if exist:
					恋人信息(起始时间、恋人信息(用户名、头像))
					新的未看到打卡信息
					新的未看的日常分享
		FLAG：
			消息信息
	*/
	if id, exist := ctx.Get("userID").(int); exist {
		userInfos, err = user_daos.UserInfoDao(id)
		if err != nil {
			return response.RespErr(ctx, err.Error())
		}
		return response.RespSuccess(ctx, "请求成功", userInfos)
	}
	return response.RespErr(ctx, "用户验证失败，请重新登录")
}

func UpdateUserInfoApi(ctx echo.Context) error {
	var (
		form user_forms.UpdateUserInfoRequest
		err  error
	)
	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	form.Pic, err = ctx.FormFile("pic")

	if userId, exist := ctx.Get("userID").(int); exist {
		err = user_daos.UpdateUserInfoDao(userId, &form)
		if err != nil {
			return response.RespErr(ctx, err.Error())
		}
		return response.RespSuccess(ctx, "操作成功", "操作成功")
	}
	return response.RespErr(ctx, "用户验证失败，请重新登录")
}

func GetNomUserInfo(ctx echo.Context) error {
	var (
		form user_forms.GetNomUserInfo
		err  error
	)
	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "数据校验失败")
	}

	userInfo, err := user_daos.GetNomUserInfo(form.UserName)
	if err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "已获取到用户信息", userInfo)
}

func InviteQrcodeApi(ctx echo.Context) error {
	if userName, exist := ctx.Get("userName").(string); exist {
		encode, err := qrcode.Encode(userName, qrcode.High, 256)
		if err != nil {
			return response.RespErr(ctx, err.Error())
		}
		return ctx.Blob(http.StatusOK, "image/png", encode)
	}
	return response.RespErr(ctx, "用户验证失败，请重新登录")
}

func SpecInviteLoverApi(ctx echo.Context) error {
	var (
		form user_forms.SpecLoverRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	if id, exist := ctx.Get("userID").(int); exist {
		err = user_daos.SpecInviteLoverDao(id, form.UserName)
		if err != nil {
			return response.RespErr(ctx, err.Error())
		}
		return response.RespSuccess(ctx, "操作成功", "操作成功")
	}

	return response.RespErr(ctx, "用户验证失败，请重新登录")
}

func InviteListApi(ctx echo.Context) error {
	if _, exist := ctx.Get("loverId").(int); exist {
		return response.RespErr(ctx, "出了点错误，你不应该看到这里的")
	}

	var (
		loverId int
		userId  int
	)
	userId = utils.GetUserId(ctx)
	loverId = utils.GetLoverId(ctx)
	if userId == 0 || loverId != 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	data, err := user_daos.ApplyListDao(int64(userId))
	if err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "操作成功", data)

}

func ApplyInviteApi(ctx echo.Context) error {
	if _, exist := ctx.Get("loverId").(int); exist {
		return response.RespErr(ctx, "出了点错误，你不应该看到这里的")
	}

	var (
		form user_forms.ApplyInviteRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	if userId, exist := ctx.Get("userID").(int); exist {
		data, err := user_daos.ApplyInviteDao(userId, &form)
		if err != nil {
			return response.RespErr(ctx, err.Error())
		}
		return response.RespSuccess(ctx, "操作成功", data)
	}

	return response.RespErr(ctx, "用户验证失败，请重新登录")
}

func UpdateLoverNameApi(ctx echo.Context) error {
	var (
		form user_forms.UpdateLoverName
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var (
		loverId int
		userId  int
	)
	userId = utils.GetUserId(ctx)
	loverId = utils.GetLoverId(ctx)
	if userId == 0 || loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	err = user_daos.UpdateLoverNameDao(loverId, userId, form.Name)
	if err != nil {
		return response.RespErr(ctx, err.Error())
	}

	return response.RespSuccess(ctx, "操作成功", "操作成功")
}

func UpdateLoverRelationInfoApi(ctx echo.Context) error {
	var (
		form user_forms.UpdateHomeNameRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if loverId, exist := ctx.Get("loverId").(int); exist {
		err := user_daos.UpdateLoverRelationInfoDao(loverId, &form)
		if err != nil {
			return response.RespErr(ctx, err.Error())
		}
		return response.RespSuccess(ctx, "操作成功", "操作成功")
	}
	return response.RespErr(ctx, "用户验证失败，请重新登录")
}
